﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace ZK.Qymd_TransManage
{
    public partial class UserUpdate : Form
    {
        string id = Login.Id;
        public UserUpdate()
        {
            InitializeComponent();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            XmlDocument document = new XmlDocument();
            document.Load("UserData.xml");
            XmlNode node = document.SelectSingleNode($"/users/user[@id = {id}]");
            XmlNodeList nodeList = document.SelectNodes("/users/user");
            foreach (XmlNode userNode in nodeList)
            {
                if (node.SelectSingleNode("name").InnerText == userNode.SelectSingleNode("name").InnerText) {
                    
                }
                else if (userNode.SelectSingleNode("name").InnerText == txtName.Text)
                {
                    MessageBox.Show("用户名已重复");
                    return;
                }
            }
            node.SelectSingleNode("name").InnerText = txtName.Text;
            node.SelectSingleNode("password").InnerText = txtPassword.Text;
            document.Save("UserData.xml");
            MessageBox.Show("修改成功");
        }

        private void UserUpdate_Load(object sender, EventArgs e)
        {
            XmlDocument document = new XmlDocument();
            document.Load("UserData.xml");
            XmlNode node = document.SelectSingleNode($"/users/user[@id = {id}]");
            if (node != null)
            {
                txtId.Text = id;
                txtName.Text = node.SelectSingleNode("name").InnerText;
                txtPassword.Text = node.SelectSingleNode("password").InnerText;
            }
            else {
                MessageBox.Show("未能获取到账号信息");
            }
        }
    }
}
